package com.dhnsoft.server;

import java.io.*;
import java.lang.reflect.Array;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description
 * @ClassName TankServer
 * @Author dhn
 * @date 2020.11.25 10:58
 */
public class TankServer{
    public static final TankServer INSTANCE=new TankServer();
    //定义一个静态变量
    private static int ID = 100;
    public static final int TCP_PORT = 8888;
    public String keyCode;


//    public static List<Integer> list = null;
//
//    public static int getList() {
//        if(list.size()==0){
//            return 0;
//        }else {
//            return list.get(list.size()-1);
//        }
//    }

//    public void setList(List<Integer> list) {
//        this.list = list;
//    }

    public void start(){
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(TCP_PORT);

        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true){
            Socket s = null;
            try {
                //list = new ArrayList<>();
                s=serverSocket.accept();
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String IP=s.getInetAddress().getHostAddress();
                keyCode = dis.readUTF();
                INSTANCE.setKeyCode(keyCode);
                //list.add(Integer.parseInt(keyCode));
                System.out.println(INSTANCE.getKeyCode());
                //getKeyCode();
//                if(list.size()!=0){
//                    System.out.println("list:"+list.get(list.size()-1));
//                }
//                    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
//                    dos.writeUTF(String.valueOf(list.get(list.size()-1)));
                File src = new File("D:/a.txt");
                PrintWriter printWriter = new PrintWriter(src);
                //OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
                // 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
                printWriter.write(keyCode);
                printWriter.flush();
                //fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
            }
        }
    }

    public String getKeyCode() {
        return keyCode;
    }

    public void setKeyCode(String keyCode) {
        this.keyCode = keyCode;
    }

    public static void main(String[] args) {
        new TankServer().start();
    }




}


